import 'package:appflutterhomeschool/model/templates_model.dart';
import 'package:appflutterhomeschool/provider/provider_widget.dart';
import 'package:appflutterhomeschool/res/styles.dart';
import 'package:appflutterhomeschool/route/application.dart';
import 'package:appflutterhomeschool/route/routes.dart';
import 'package:appflutterhomeschool/ui/widgets/notice/new_notification_view_model.dart';
import 'package:appflutterhomeschool/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'custum_notifications_page.dart';

///用户： ninetailedfox
///时间： 2020/6/20
///邮箱： 1037438704@qq.com
///功能： 新建通知

class NewNotificationPage extends StatelessWidget {


  @override
  Widget build(BuildContext context) {
    List<Widget> listTab = [
      Tab(text: "全部"),
      Tab(text: "热门"),
      Tab(text: "活动"),
      Tab(text: "班级"),
      Tab(text: "安全"),
      Tab(text: "假期"),
    ];
    return DefaultTabController(
      length: listTab.length,
      child: Scaffold(
        backgroundColor: Colors.grey[100],
        appBar: AppBar(
          brightness: Brightness.light,
          elevation: 4,
          centerTitle: true,
          title: Text("通知模版", style: TextStyles.textDarkBold43),
          leading: new IconButton(
            icon: Icon(Icons.arrow_back_ios,
                size: ScreenUtil().setSp(40), color: Colors.black),
            onPressed: () => Navigator.pop(context),
          ),
        ),
        body: Column(
          children: <Widget>[
            Container(
              color: Colors.white,
              child: Row(
                children: <Widget>[
                  TabBar(
                    indicatorSize: TabBarIndicatorSize.label,
                    indicatorWeight: 2,
                    //选中的icon颜色
                    labelPadding: EdgeInsets.only(
                        left: ScreenUtil().setWidth(67 / 2),
                        right: ScreenUtil().setWidth(67 / 2)),
                    indicatorColor: Colors.blue,
                    isScrollable: true,
                    labelColor: Colors.black,
                    labelStyle: TextStyles.textDark37,
                    unselectedLabelStyle: TextStyles.textGray37,
                    unselectedLabelColor: Colors.grey,
                    tabs: listTab,
                  ),
                  Spacer()
                ],
              ),
            ),
            Expanded(
              child: TabBarView(
                children: <Widget>[
                  NewNotificationTabBarView(0),
                  NewNotificationTabBarView(1),
                  NewNotificationTabBarView(2),
                  NewNotificationTabBarView(3),
                  NewNotificationTabBarView(4),
                  NewNotificationTabBarView(5),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}

class NewNotificationTabBarView extends StatelessWidget {
  final int id;

  NewNotificationTabBarView(this.id);

  @override
  Widget build(BuildContext context) {
    return ProviderWidget<NewNotificationViewModel>(
      model: NewNotificationViewModel(context, id),
      onModelReady: (value) => value.initView(),
      builder: (context, viewModel, child) {
        return GridView.builder(
          shrinkWrap: true,
          itemCount: viewModel.templatesList.length,
          padding: EdgeInsets.all(ScreenUtil().setWidth(35)),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            mainAxisSpacing: 5,
            crossAxisSpacing: 5,
            childAspectRatio: 7 / 8,
          ),
          itemBuilder: (context, index) {
            TemplatesModel templatesModel = viewModel.templatesList[index];
            return Padding(
              padding: EdgeInsets.all(2),
              child: Material(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10),
                elevation: 3,
                child: InkWell(
                  child: Container(
                    padding: EdgeInsets.only(
                      top: ScreenUtil().setWidth(53),
                      bottom: ScreenUtil().setWidth(53),
                      left: ScreenUtil().setWidth(31),
                      right: ScreenUtil().setWidth(31),
                    ),
                    decoration:
                        BoxDecoration(borderRadius: BorderRadius.circular(10)),
                    child: index == 0
                        ? GestureDetector(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: <Widget>[
                                Image.asset(
                                    Utils.getImgPath("fabu_tongzhi_zdy")),
                                SizedBox(height: ScreenUtil().setWidth(21)),
                                Text(
                                  "自定义通知",
                                  style: TextStyles.textGray31,
                                ),
                              ],
                            ),
                            onTap: () {
                              if (index == 0) {
                                Application().navigatePushToPage(context,
                                    Routes.custumNotificationsPage, {"pageTag": 0});
                              }
                            },
                          )
                        : Column(
                            children: <Widget>[
                              Text(
                                templatesModel?.title ?? "",
                                style: TextStyles.textDarkBold43,
                              ),
                              SizedBox(height: ScreenUtil().setWidth(43)),
                              Text(templatesModel?.msg ?? "",
                                  style: TextStyles.textGray31),
                            ],
                          ),
                  ),
                  borderRadius: BorderRadius.circular(10),
                  onTap: () {
                    Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) =>
                                CustumNotificationsPage(
                                    pageTag: 0,
                                    templatesModel: templatesModel)));
                  },
                ),
              ),
            );
          },
        );
      },
    );
  }
}
